package com.uwlaser.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.domain.DataMaterialBatch;
import com.uwlaser.domain.DataMaterialSingle;
import com.uwlaser.service.dto.DataMaterialBatchDto;
import com.uwlaser.service.mapper.DataMaterialBatchMapper;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DataMaterialBatchHistory;
import com.uwlaser.service.DataMaterialBatchHistoryService;
import com.uwlaser.service.dto.DataMaterialBatchHistoryDto;
import com.uwlaser.service.dto.DataMaterialBatchHistoryQueryParam;
import com.uwlaser.service.mapper.DataMaterialBatchHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DataMaterialBatchHistoryService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DataMaterialBatchHistoryServiceImpl extends CommonServiceImpl<DataMaterialBatchHistoryMapper, DataMaterialBatchHistory> implements DataMaterialBatchHistoryService {

    // private final RedisUtils redisUtils;
    private final DataMaterialBatchHistoryMapper dataMaterialBatchHistoryMapper;
    private final DataMaterialBatchMapper dataMaterialBatchMapper;

    @Override
    public PageInfo<DataMaterialBatchHistoryDto> queryAll(DataMaterialBatchHistoryQueryParam query, Pageable pageable) {
        IPage<DataMaterialBatchHistory> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DataMaterialBatchHistory> page = dataMaterialBatchHistoryMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DataMaterialBatchHistoryDto.class);
    }

    @Override
    public List<DataMaterialBatchHistoryDto> queryAll(DataMaterialBatchHistoryQueryParam query){
        return ConvertUtil.convertList(dataMaterialBatchHistoryMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DataMaterialBatchHistoryDto.class);
    }

    @Override
    public DataMaterialBatchHistory getById(Integer id) {
        return dataMaterialBatchHistoryMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DataMaterialBatchHistoryDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DataMaterialBatchHistoryDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DataMaterialBatchHistoryDto resources) {
        DataMaterialBatchHistory entity = ConvertUtil.convert(resources, DataMaterialBatchHistory.class);
        return dataMaterialBatchHistoryMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DataMaterialBatchHistoryDto resources){
        DataMaterialBatchHistory entity = ConvertUtil.convert(resources, DataMaterialBatchHistory.class);
        int ret = dataMaterialBatchHistoryMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return dataMaterialBatchHistoryMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int recover(List<DataMaterialBatchHistoryDto> resources) {
        int result = 0;
        // 删除物料批次历史 和 新增物料批次
        result = dataMaterialBatchHistoryMapper.deleteBatchIds(resources.stream().map(DataMaterialBatchHistoryDto::getId).collect(Collectors.toSet()));
        resources.forEach( dataMaterialBatchHistory -> {
            dataMaterialBatchHistory.setId(null);
            dataMaterialBatchMapper.insert(ConvertUtil.convert(dataMaterialBatchHistory,DataMaterialBatch.class));
        });
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DataMaterialBatchHistoryDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DataMaterialBatchHistoryDto dataMaterialBatchHistory : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("产品SN", dataMaterialBatchHistory.getProductSn());
              map.put("产品型号", dataMaterialBatchHistory.getProductCode());
              map.put("产品类型", dataMaterialBatchHistory.getProductType());
              map.put("订单编号", dataMaterialBatchHistory.getOrderCode());
              map.put("工单编号", dataMaterialBatchHistory.getWorkOrderCode());
              map.put("产线编号", dataMaterialBatchHistory.getLienCode());
              map.put("工段编号", dataMaterialBatchHistory.getSectionCode());
              map.put("工位编号", dataMaterialBatchHistory.getStationCode());
              map.put("工序编号", dataMaterialBatchHistory.getOperationCode());
              map.put("设备编号", dataMaterialBatchHistory.getDeviceCode());
              map.put("物料编号", dataMaterialBatchHistory.getMaterialSn());
              map.put("物料名称", dataMaterialBatchHistory.getMaterialName());
              map.put("物料编码", dataMaterialBatchHistory.getMaterialCode());
              map.put("使用数量", dataMaterialBatchHistory.getQuantity());
              map.put("物料单位", dataMaterialBatchHistory.getMaterialUnit());
              map.put("过站ID", dataMaterialBatchHistory.getPassStationId());
              map.put("创建人", dataMaterialBatchHistory.getCreateBy());
              map.put("创建时间", dataMaterialBatchHistory.getCreateTime());
              map.put("更新人", dataMaterialBatchHistory.getUpdateBy());
              map.put("更新时间", dataMaterialBatchHistory.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
